    /**
 *  Copyright (c) 2010 The edtf project.  All rights reserved.
 *
 *  This file is part of edtf (Event Driven Testing Framework).
 *
 *  edtf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  edtf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with edtf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/edtf.
 */
package org.xepf.script;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import org.xepf.utils.ObjectCreator;
import org.xepf.xml.adapter.NodeSerializer;
import org.xepf.xml.adapter.XPath;
import org.xepf.xml.adapter.XSLT;
import org.xepf.functions.QueryBoard;

/**
 *
 *
 */
public class Scriplet implements Runnable {

    private String name = "unknown";
    private boolean isMainScript = false;
    private String translator;
    private String scriplet;
    private String result = "";
    private ExecutionContext executionContext;
    private XSLT transformer;

    public Scriplet() throws Exception {
        transformer = (XSLT) ObjectCreator.get("XSLT");
    }

    public void initialize() throws Exception {
        XPath xPath;
        NodeSerializer s;

        xPath = (XPath) ObjectCreator.get("XPath");
        s = (NodeSerializer) ObjectCreator.get("NodeSerializer");

        xPath.initialize(scriplet);
        name = s.serialize(xPath.query("/scriplet/@name"));
    }

    @Override
    public void run() {
        try {
            List<ExtensionFunctionDefinition> extensions = new LinkedList<ExtensionFunctionDefinition>();
            extensions.add(new QueryBoard( executionContext ));
            //extensions.add(new CallAdapter( executionContext ));
            //extensions.add(new Post( executionContext ));
            transformer.registerExtensions(extensions);
            transformer.initialize(translator, scriplet);

            if (executionContext.hasRealScripletThreading()) {
                Logger.getLogger(ScriptPackage.class.getName()).log(Level.INFO, "Scriplet '" + name + "' started.");
            }
            
            String pureXslScript = transformer.transform();
            Logger.getLogger(ScriptPackage.class.getName()).log(Level.FINE, "Transformer result: ");
            Logger.getLogger(ScriptPackage.class.getName()).log(Level.FINE, pureXslScript);
            Logger.getLogger(ScriptPackage.class.getName()).log(Level.FINE,
                    "----------------------------------------------------");

            transformer.initialize(pureXslScript, executionContext.getSampleEvents());
            result = transformer.transform();
        } catch (Exception ex) {
            Logger.getLogger(ScriptPackage.class.getName()).log(Level.SEVERE, ex.toString());
        }
    }

    public void setExecutionContext(ExecutionContext executionContext) {
        this.executionContext = executionContext;
    }

    public void setTranslator(String translator) {
        this.translator = translator;
    }

    public void setScriplet(String scriplet) {
        this.scriplet = scriplet;
    }

    public void setMainScriplet(boolean isMainScript) {
        this.isMainScript = isMainScript;
    }

    public boolean isMainScriplet() {
        return isMainScript;
    }

    public String getName() {
        return name;
    }

    public String getResult() {
        return result;
    }

    public void joinChildScriplets() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
